package com.cvte.boe.media.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.cvte.boe.media.R;
import com.cvte.boe.media.model.LocalDeviceInfo;
import com.cvte.boe.media.utils.OrientationUtil;
import com.cvte.boe.media.views.widget.MarqueeTextView;

import java.util.List;

/**
 * Created by wuguitong on 2018/4/8.
 */
public class UsbDevAdapter extends RecyclerViewClickAndSelectAdapter<UsbDevAdapter.UsbViewHolder>{
    private Context mContext;
    private List<LocalDeviceInfo> mLocalDevList;
    private String mFocusPositionMountPath;
    private int mCurrentPosition;

    public UsbDevAdapter(Context mContext, List<LocalDeviceInfo> localDevList) {
        this.mContext = mContext;
        this.mLocalDevList = localDevList;
        mFocusPositionMountPath = "";
        mCurrentPosition = 0;
    }

    public void setLocalDevList(List<LocalDeviceInfo> localDevList){
        this.mLocalDevList = localDevList;
    }

    @Override
    public UsbViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if(OrientationUtil.isLandscapeOrientation(mContext)){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_usbdev_view_land,parent,false);
        }else{
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_usbdev_view_port,parent,false);
        }
        UsbViewHolder viewHolder = new UsbViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(UsbViewHolder holder, int position) {
        if(mLocalDevList != null) {
            holder.ivUsbRes.setImageResource(R.drawable.icon_usb);
            holder.tvUsbName.setText(mLocalDevList.get(position).getDevName());
            holder.tvfreeSize.setText(mLocalDevList.get(position).getFreeSize());
            holder.tvtotalSize.setText(mLocalDevList.get(position).getTotalSize());
            if(mLocalDevList.get(position).getMountPath().equals(mFocusPositionMountPath)){
                holder.itemView.requestFocus();
            }
            if(position == mCurrentPosition){
                holder.tvUsbName.setMarqueeState(true);
                holder.tvfreeSize.setMarqueeState(true);
                holder.tvtotalSize.setMarqueeState(true);
            }else{
                holder.tvUsbName.setMarqueeState(false);
                holder.tvfreeSize.setMarqueeState(false);
                holder.tvtotalSize.setMarqueeState(false);
            }
        }
        super.onBindViewHolder(holder,position);
    }

    @Override
    public int getItemCount() {
        return (mLocalDevList != null) ?mLocalDevList.size():0;
    }

    class UsbViewHolder extends RecyclerView.ViewHolder{
        ImageView ivUsbRes;
        MarqueeTextView tvUsbName;
        MarqueeTextView tvfreeSize;
        MarqueeTextView tvtotalSize;

        public UsbViewHolder(View itemView) {
            super(itemView);
            this.ivUsbRes = (ImageView)itemView.findViewById(R.id.iv_usbdev_image);
            this.tvUsbName = (MarqueeTextView) itemView.findViewById(R.id.tv_usbdev_name);
            this.tvfreeSize = (MarqueeTextView) itemView.findViewById(R.id.tv_usbdev_free_size);
            this.tvtotalSize = (MarqueeTextView) itemView.findViewById(R.id.tv_usbdev_total_size);
        }
    }
    public void setFocusPositionMountPath(String  path){
            this.mFocusPositionMountPath = path;
    }
    public  void setSelectPosition(int position){
        mCurrentPosition = position;
    }
    public  int getSelectPosition(){
        return mCurrentPosition;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
}
